
package cn.com.pusai.core.config;

import cn.com.pusai.core.util.IOUtil;
import cn.com.pusai.core.util.StringUtil;
import java.io.*;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// Referenced classes of package cn.com.ecinc.core.config:
//			PropertyExpander

public abstract class AbstractConfig
{

	private static Logger logger = LoggerFactory.getLogger(cn.com.pusai.core.config.AbstractConfig.class);
	private String configId;
	private Map config;
	private static Map allCustomConfig;
	public static final String CONFIG_BASE_PATH = "/";
	public static final String CUSTOM_CONFIG_XML_FILE_NAME = "custom.xml";
	public static final String CUSTOM_CONFIG_FILE_NAME = "custom.properties";

	protected AbstractConfig()
	{
		init();
	}

	private void init()
	{
		configId = getConfigId();
		config = getDefaultConfig();
		if (config == null)
			config = new HashMap();
		Map customConfig = getCustomConfig();
		if (customConfig != null)
		{
			String key;
			for (Iterator iterator = customConfig.keySet().iterator(); iterator.hasNext(); config.put(key, (String)customConfig.get(key)))
				key = (String)iterator.next();

		}
	}

	protected abstract String getConfigId();

	protected Map getDefaultConfig()
	{
		Properties config;
		String configFile;
		java.io.InputStream is;
		config = new Properties();
		configFile = (new StringBuilder("/")).append(configId).append(".xml").toString();
		is = null;
		try
		{
			is = getClass().getResourceAsStream(configFile);
			if (is != null)
			{
				config.loadFromXML(is);
			} else
			{
				configFile = (new StringBuilder("/")).append(configId).append(".properties").toString();
				is = getClass().getResourceAsStream(configFile);
				config.load(is);
			}
		}
		catch (Exception e)
		{
			throw new RuntimeException((new StringBuilder("未找到配置文件或配置文件有错:")).append(configFile).toString(), e);
		}
		
		IOUtil.close(is);
		IOUtil.close(is);
		
		Map map = config;
		expandProperty(config);
		return map;
	}

	private Map getCustomConfig()
	{
		String s = "/";
		
		java.io.InputStream input;
		boolean isXmlConfigFile;
		if (allCustomConfig != null)
		
		allCustomConfig = new HashMap();
		input = null;
		isXmlConfigFile = false;
		String customFilePath = System.getProperty("config.custom.file");
		if (customFilePath != null)
		{
			File customFile = new File(customFilePath);
			if (customFile.isFile())
				try
				{
					input = new FileInputStream(customFile);
					if (StringUtil.equalsIgnoreCase("xml", IOUtil.getFileExtension(customFile)))
						isXmlConfigFile = true;
					logger.info((new StringBuilder("在文件系统中找到自定义配置文件:")).append(customFile.getAbsolutePath()).toString());
				}
				catch (FileNotFoundException filenotfoundexception) { }
			else
				logger.error((new StringBuilder("未找到自定义配置文件:")).append(customFile.getAbsolutePath()).toString());
		} else
		if ((input = getClass().getResourceAsStream("/custom.xml")) != null)
		{
			logger.info("在类路径下找到自定义配置文件://custom.xml");
			isXmlConfigFile = true;
		} else
		if ((input = getClass().getResourceAsStream("/custom.properties")) != null)
			logger.info("在类路径下找到自定义配置文件:/custom.properties");
		else
		if ((input = getClass().getResourceAsStream("/custom.xml")) != null)
		{
			logger.info("在类路径下找到自定义配置文件:/custom.xml");
			isXmlConfigFile = true;
		} else
		if ((input = getClass().getResourceAsStream("/custom.properties")) != null)
			logger.info("在类路径下找到自定义配置文件:/custom.properties");
		if (input == null)
		{
			logger.info("未应用自定义配置文件.");
		} else
		{
			Properties config = new Properties();
			try
			{
				if (isXmlConfigFile)
					config.loadFromXML(input);
				else
					config.load(input);
			}
			catch (IOException e)
			{
				throw new RuntimeException(e);
			}
			expandProperty(config);
			for (Iterator iterator = config.keySet().iterator(); iterator.hasNext();)
			{
				Object obj = iterator.next();
				String key = (String)obj;
				int dotIndex = key.indexOf(".");
				if (dotIndex > 0 && dotIndex != key.length() - 1)
				{
					String configId = key.substring(0, dotIndex);
					String subKey = key.substring(dotIndex + 1);
					Map configMap = (Map)allCustomConfig.get(configId);
					if (configMap == null)
					{
						configMap = new HashMap();
						allCustomConfig.put(configId, configMap);
					}
					configMap.put(subKey, (String)config.get(key));
				} else
				{
					logger.warn((new StringBuilder("忽略key:")).append(key).toString());
				}
			}

		}
		
		IOUtil.close(input);
		
		IOUtil.close(input);
		

		return (Map)allCustomConfig.get(this.configId);
	}

	private void expandProperty(Properties properties)
	{
		Map temp = properties;
		Map map = temp;
		String key;
		String value;
		for (Iterator iterator = map.keySet().iterator(); iterator.hasNext(); map.put(key, value))
		{
			key = (String)iterator.next();
			value = PropertyExpander.expandProperties((String)map.get(key), map);
			value = PropertyExpander.expandSystemProperties(value);
		}

	}

	protected void setProperty(String key, String value)
	{
		config.put(key, value);
	}

	public String getProperty(String key)
	{
		return (String)config.get(key);
	}

	public String getProperty(String key, String defaultValue)
	{
		String val = getProperty(key);
		return val != null ? val : defaultValue;
	}

	public boolean getProperty(String key, boolean defaultValue)
	{
		String val = getProperty(key);
		return val != null ? Boolean.valueOf(val).booleanValue() : defaultValue;
	}

	public int getProperty(String key, int defaultValue)
	{
		String val = getProperty(key);
		return val != null ? Integer.parseInt(val) : defaultValue;
	}

	public Integer getProperty(String key, Integer defaultValue)
	{
		String val = getProperty(key);
		return val != null ? Integer.valueOf(val) : defaultValue;
	}

	public long getProperty(String key, long defaultValue)
	{
		String val = getProperty(key);
		return val != null ? Long.parseLong(val) : defaultValue;
	}

	public Long getProperty(String key, Long defaultValue)
	{
		String val = getProperty(key);
		return val != null ? Long.valueOf(val) : defaultValue;
	}

	public float getProperty(String key, float defaultValue)
	{
		String val = getProperty(key);
		return val != null ? Float.parseFloat(val) : defaultValue;
	}

	public Float getProperty(String key, Float defaultValue)
	{
		String val = getProperty(key);
		return val != null ? Float.valueOf(val) : defaultValue;
	}

	public double getProperty(String key, double defaultValue)
	{
		String val = getProperty(key);
		return val != null ? Double.parseDouble(val) : defaultValue;
	}

	public Double getProperty(String key, Double defaultValue)
	{
		String val = getProperty(key);
		return val != null ? Double.valueOf(val) : defaultValue;
	}

	public Map getAllProperties()
	{
		return Collections.unmodifiableMap(config);
	}

}
